package com.jinglvhuanjing.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;

import com.jinglvhuanjing.app.base.CountDownActivity;
import com.jinglvhuanjing.app.bean.LoginEvent;
import com.jinglvhuanjing.app.bean.RunEvent;
import com.jinglvhuanjing.app.utils.ByteUtil;
import com.jinglvhuanjing.app.utils.Constants;
import com.jinglvhuanjing.app.utils.SerialMsgHandler;
import com.jinglvhuanjing.app.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class LoginActivity extends CountDownActivity implements View.OnClickListener
{
    public static void startActivity(Activity activity){
        Intent intent = new Intent(activity , LoginActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initCountDownText() {
        this.mCountText = findViewById(R.id.count_down);
    }

    private FrameLayout right_yk;
    private AppCompatTextView number;
    private AppCompatButton back_btn;
    private AppCompatButton key_0, key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9;
    private AppCompatButton key_del, key_login;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        setOnResume(true);
        this.right_yk = findViewById(R.id.right_yk);
        this.number = findViewById(R.id.number);
        this.back_btn = findViewById(R.id.back_btn);
        this.key_0 = findViewById(R.id.key_0);
        this.key_1 = findViewById(R.id.key_1);
        this.key_2 = findViewById(R.id.key_2);
        this.key_3 = findViewById(R.id.key_3);
        this.key_4 = findViewById(R.id.key_4);
        this.key_5 = findViewById(R.id.key_5);
        this.key_6 = findViewById(R.id.key_6);
        this.key_7 = findViewById(R.id.key_7);
        this.key_8 = findViewById(R.id.key_8);
        this.key_9 = findViewById(R.id.key_9);
        this.key_del = findViewById(R.id.key_del);
        this.key_login = findViewById(R.id.key_login);

        back_btn.setOnClickListener(this);
        key_0.setOnClickListener(this);
        key_1.setOnClickListener(this);
        key_2.setOnClickListener(this);
        key_3.setOnClickListener(this);
        key_4.setOnClickListener(this);
        key_5.setOnClickListener(this);
        key_6.setOnClickListener(this);
        key_7.setOnClickListener(this);
        key_8.setOnClickListener(this);
        key_9.setOnClickListener(this);
        key_del.setOnClickListener(this);
        key_login.setOnClickListener(this);
        this.initEvent();
        setLeftView();

        EventBus.getDefault().register(this);
    }

    @Subscribe
    public void onEvent(LoginEvent event) {
        //将来数据写入主线程
        Message message = new Message();
        message.obj = event.getValue();
        message.what = SerialMsgHandler.CONNECT3;
        this.mHandlerLogin.sendMessage(message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.mHandlerLogin.removeMessages(2);
        setOnResume(false);
        EventBus.getDefault().unregister(this);
    }

    private void initEvent(){
        this.right_yk.setOnClickListener(v-> {
            GarbageDisposalActivity.startActivity(LoginActivity.this);
            finish();
        });
    }

    private void setNumber(String number_) {
        String text = number.getText().toString();
        if (text.length() == 20) {
            number.setText(text.substring(0, text.length() - 1) + number_);
        } else {
            number.append(number_);
        }
    }

    @Override
    public void countDownComplete() {
        finish();
    }

    @Override
    public void onClick(View view) {
        String number_ = ((AppCompatButton) view).getText().toString();
        String number_n = number.getText().toString();
        switch (view.getId()){
            case R.id.back_btn:
                finish();
                break;
            case R.id.key_0:
            case R.id.key_1:
            case R.id.key_2:
            case R.id.key_3:
            case R.id.key_4:
            case R.id.key_5:
            case R.id.key_6:
            case R.id.key_7:
            case R.id.key_8:
            case R.id.key_9:
                setNumber(number_);
                break;
            case R.id.key_del:
                if(number_n.length()>0){
                    String number_1 = number_n.substring(0, number_n.length()-1);
                    number.setText(number_1);
                }
                break;
            case R.id.key_login:
                String account = SpUtils.getString(getApplicationContext(), Constants.ACCOUNT);
                if(number_n.length()>0){
                    if(account.contains(number_n)){
                        GarbageDisposalActivity.startActivity(LoginActivity.this);
                        finish();
                    }else{
                        Toast.makeText(getApplicationContext(), "账号不正确", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }
}
